package 极客时间.concurrence.并发编程实战篇.手写一把lock;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;

/**
 * @Author idea
 * @Date created in 11:05 下午 2020/7/22
 */
public class IdeaLockV2 implements Lock {

    private BacAQS bacAQS = new BacAQS() {
        @Override
        public boolean tryAcquire() {
            return owner.compareAndSet(null,Thread.currentThread());
        }

        @Override
        public boolean tryRelease() {
            return owner.compareAndSet(Thread.currentThread(),null);
        }
    };

    @Override
    public void lock() {
        bacAQS.acquire();
    }

    @Override
    public void lockInterruptibly() throws InterruptedException {

    }

    @Override
    public boolean tryLock() {
        return bacAQS.tryAcquire();
    }

    @Override
    public boolean tryLock(long time, TimeUnit unit) throws InterruptedException {
        return false;
    }

    @Override
    public void unlock() {
        bacAQS.release();
    }

    @Override
    public Condition newCondition() {
        return null;
    }
}
